
package com.x52im.mall.logic.shop;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;

import com.x52im.rbchat.R;
import com.x52im.mall.Utility;
import com.eva.epc.common.util.CommonUtils;

/**
 * 金额合计等包装实现类.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public class OrderComfirmAmountWrapper
{
	private Activity parentActivity = null;

	/** 标签组件：商品金额货币类型 */
	private TextView viewGoodsAmountCurrency = null;
	/** 标签组件：商品金额（不含运费） */
	private TextView viewGoodsAmount = null;
	/** 标签组件：运费金额货币类型 */
	private TextView viewTransferFareCurrency = null;
	/** 标签组件：运费金额 */
	private TextView viewTransferFare = null;
	/** 标签组件：订单金额货币类型 */
	private TextView viewTotalAmountCurrency = null;
	/** 标签组件：订单金额（含运费） */
	private TextView viewTotalAmount = null;

	public OrderComfirmAmountWrapper(Activity parentActivity)
	{
		this.parentActivity = parentActivity;

		initViews();
		initListeners();
	}

	/**
	 * UI初始化核心方法.
	 */
	private void initViews()
	{
		viewGoodsAmountCurrency = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_goodsAmountCurrencyView);
		viewGoodsAmount = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_goodsAmountView);
		viewTransferFareCurrency = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_transferFareCurrencyView);
		viewTransferFare = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_transferFareView);
		viewTotalAmountCurrency = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_totalAmountCurrencyView);
		viewTotalAmount = (TextView)parentActivity
				.findViewById(R.id.common_mall_shop_layout_order_confirm_amount_totalAmountView);

		//** 默认货币类型显示
		viewGoodsAmountCurrency.setText(Utility.DEFAULT_CURRENCY_TYPE_NAME);
		viewTransferFareCurrency.setText(Utility.DEFAULT_CURRENCY_TYPE_NAME);
		viewTotalAmountCurrency.setText(Utility.DEFAULT_CURRENCY_TYPE_NAME);
	}
	
	/**
	 * UI组件监听器初始化方法.
	 */
	private void initListeners()
	{
		// ** 商品总价、物流费用UI组件的文本变更时立即更新应付总金额
		TextWatcher tw = new TextWatcher(){
			@Override
			public void beforeTextChanged(CharSequence s, int start,
					int count, int after){
				//
			}
			@Override
			public void onTextChanged(CharSequence s, int start,
					int before, int count){
				//
			}

			@Override
			public void afterTextChanged(Editable s)
			{
				refreshTotalAmount();
			}
		};
		viewGoodsAmount.addTextChangedListener(tw);
		viewTransferFare.addTextChangedListener(tw);
	}

	/**
	 * 刷新应付总金额在UI上的显示.
	 * 
	 * @param goodsAmount
	 */
	public void refreshGoodsAmount(double goodsAmount)
	{
		viewGoodsAmount.setText(String.valueOf(goodsAmount));
	}

	/**
	 * 刷新物流费用在UI上的显示.
	 * 
	 * @param transferFare
	 */
	public void refreshTransferFare(double transferFare)
	{
		viewTransferFare.setText(String.valueOf(transferFare));
	}
	
	public String getTotalAmount()
	{
		return viewTotalAmount.getText().toString();
	}

	/**
	 * 更新总金额 = 商品总金额  + 运费。
	 */
	private void refreshTotalAmount()
	{
		viewTotalAmount.setText(
				CommonUtils.getScaledValue(CommonUtils.getDoubleValue(viewGoodsAmount.getText().toString())
						+ CommonUtils.getDoubleValue(viewTransferFare.getText().toString()), 2, false)
				);
	}
}